import SwiftUI
import Photos

struct PhotoPermissionView: View {
    @ObservedObject var photoService: PhotoService
    @State private var isRequesting = false
    
    var body: some View {
        VStack(spacing: 30) {
            Spacer()
            
            // 图标
            Image(systemName: "photo.on.rectangle.angled")
                .font(.system(size: 80))
                .foregroundColor(.blue)
                .padding(.bottom, 20)
            
            // 标题
            Text("访问您的照片")
                .font(.largeTitle)
                .fontWeight(.bold)
                .multilineTextAlignment(.center)
            
            // 说明文字
            VStack(spacing: 16) {
                Text("PhotoTour 需要访问您的照片库来为您提供智能的照片管理服务")
                    .font(.body)
                    .multilineTextAlignment(.center)
                    .foregroundColor(.secondary)
                
                VStack(alignment: .leading, spacing: 12) {
                    PermissionFeatureRow(
                        icon: "location.circle.fill",
                        title: "地点回忆",
                        description: "根据照片中的位置信息，为您整理旅行回忆"
                    )
                    
                    PermissionFeatureRow(
                        icon: "calendar.circle.fill",
                        title: "时间轴",
                        description: "按时间顺序展示您的照片，重温美好时光"
                    )
                    
                    PermissionFeatureRow(
                        icon: "sparkles",
                        title: "智能分析",
                        description: "自动识别相似照片，帮您整理相册"
                    )
                }
                .padding(.horizontal, 20)
            }
            
            Spacer()
            
            // 权限状态显示
            if photoService.authorizationStatus != .notDetermined {
                PermissionStatusView(status: photoService.authorizationStatus)
            }
            
            // 按钮
            VStack(spacing: 16) {
                if photoService.authorizationStatus == .notDetermined {
                    Button(action: requestPermission) {
                        HStack {
                            if isRequesting {
                                ProgressView()
                                    .scaleEffect(0.8)
                                    .foregroundColor(.white)
                            } else {
                                Image(systemName: "photo.fill")
                            }
                            Text(isRequesting ? "请求中..." : "允许访问照片")
                        }
                        .frame(maxWidth: .infinity)
                        .frame(height: 50)
                        .background(Color.blue)
                        .foregroundColor(.white)
                        .cornerRadius(12)
                    }
                    .disabled(isRequesting)
                } else if photoService.authorizationStatus == .denied || photoService.authorizationStatus == .restricted {
                    Button(action: openSettings) {
                        HStack {
                            Image(systemName: "settings")
                            Text("前往设置")
                        }
                        .frame(maxWidth: .infinity)
                        .frame(height: 50)
                        .background(Color.orange)
                        .foregroundColor(.white)
                        .cornerRadius(12)
                    }
                }
                
                if photoService.authorizationStatus == .denied || photoService.authorizationStatus == .restricted {
                    Text("请在设置中允许 PhotoTour 访问您的照片")
                        .font(.caption)
                        .foregroundColor(.secondary)
                        .multilineTextAlignment(.center)
                }
            }
            .padding(.horizontal, 30)
            
            Spacer()
        }
        .padding()
        .background(Color(.systemBackground))
    }
    
    private func requestPermission() {
        isRequesting = true
        
        Task {
            await photoService.requestAuthorization()
            
            await MainActor.run {
                isRequesting = false
            }
        }
    }
    
    private func openSettings() {
        if let settingsUrl = URL(string: UIApplication.openSettingsURLString) {
            UIApplication.shared.open(settingsUrl)
        }
    }
}

struct PermissionFeatureRow: View {
    let icon: String
    let title: String
    let description: String
    
    var body: some View {
        HStack(spacing: 12) {
            Image(systemName: icon)
                .font(.title2)
                .foregroundColor(.blue)
                .frame(width: 24)
            
            VStack(alignment: .leading, spacing: 4) {
                Text(title)
                    .font(.headline)
                    .foregroundColor(.primary)
                
                Text(description)
                    .font(.caption)
                    .foregroundColor(.secondary)
            }
            
            Spacer()
        }
    }
}

struct PermissionStatusView: View {
    let status: PHAuthorizationStatus
    
    var body: some View {
        HStack(spacing: 8) {
            Image(systemName: statusIcon)
                .foregroundColor(statusColor)
            
            Text(statusMessage)
                .font(.caption)
                .foregroundColor(statusColor)
        }
        .padding(.horizontal, 16)
        .padding(.vertical, 8)
        .background(statusColor.opacity(0.1))
        .cornerRadius(8)
    }
    
    private var statusIcon: String {
        switch status {
        case .authorized, .limited:
            return "checkmark.circle.fill"
        case .denied, .restricted:
            return "xmark.circle.fill"
        case .notDetermined:
            return "questionmark.circle.fill"
        @unknown default:
            return "questionmark.circle.fill"
        }
    }
    
    private var statusColor: Color {
        switch status {
        case .authorized, .limited:
            return .green
        case .denied, .restricted:
            return .red
        case .notDetermined:
            return .orange
        @unknown default:
            return .orange
        }
    }
    
    private var statusMessage: String {
        switch status {
        case .authorized:
            return "已获得完整访问权限"
        case .limited:
            return "已获得有限访问权限"
        case .denied:
            return "访问被拒绝"
        case .restricted:
            return "访问被限制"
        case .notDetermined:
            return "等待权限请求"
        @unknown default:
            return "未知状态"
        }
    }
}

#Preview {
    PhotoPermissionView(photoService: PhotoService())
} 